import hashlib
from django.shortcuts import render
from django.http import HttpResponse
from .models import UserProfile


# Create your views here.


def pwd_md5(password):
    m = hashlib.md5()
    m.update(password.encode())
    return m.hexdigest()


def test_view(request):
    # user = UserProfile.objects.create(username='yufengguang', password='123456', email='2523810765@qq.com', phone='17685457791')
    # print('--->', user)

    # result = UserProfile.objects.filter(
    #     username='zhaoliying'
    # )
    # print('---------->>>', result)
    # print('---------->>>', result[0].password)

    user = UserProfile.objects.get(
        username='zhaoliying'
    )
    print('-------->>>', user.email)

    return HttpResponse("Hello Word!!!")


def register_view(request):
    if request.method == "GET":
        return render(request, "register.html")
    elif request.method == "POST":
        data = request.POST
        username = data.get("username")
        password = data.get("password")
        password_again = data.get("password2")
        email = data.get("email")
        phone = data.get("phone")
        if password != password_again:
            return HttpResponse("Error")
        user_query = UserProfile.objects.filter(
            username=username
        )
        if user_query:
            return HttpResponse("Occupied")

        UserProfile.objects.create(
            username=username,
            password=pwd_md5(password),
            email=email,
            phone=phone
        )
        return HttpResponse("Success")


def login_view(request):
    if request.method == "GET":
        return render(request, "login.html")
    elif request.method == "POST":
        data = request.POST
        username = data.get("username")
        password = data.get("password")

        try:
            user = UserProfile.objects.get(
                username=username
            )
        except Exception as e:
            return HttpResponse("Account error")

        if pwd_md5(password) != user.password:
            return HttpResponse("Password error")
        return HttpResponse("Success~")


def update_view(request):
    if request.method == "GET":
        return render(request, "update.html")
    elif request.method == "POST":
        data = request.POST
        username = data.get("username")
        password = data.get("password")
        new_password = data.get("new_password")
        user_query = UserProfile.objects.filter(
            username=username
        )
        if not user_query:
            return HttpResponse("Account error")

        user = user_query[0]
        if user.password != pwd_md5(password):
            return HttpResponse("password error")

        user.password = pwd_md5(new_password)
        user.save()

        return HttpResponse("update success")


def delete_view(request):
    if request.method == "GET":
        return render(request, "delete.html")
    elif request.method == "POST":
        data = request.POST
        username = data.get("username")
        password = data.get("password")

    try:
        user = UserProfile.objects.get(
            username=username
        )
    except Exception as e:
        return HttpResponse("Not exit")

    if user.password != pwd_md5(password):
        return HttpResponse("error")

    user.delete()

    return HttpResponse("Delete success")
